package init;

import init.utils.ExecuteUtils;
import init.utils.SqlScriptUtils;
import init.utils.WorkflowUtils;

import java.io.File;
import java.net.URISyntaxException;
import java.sql.SQLException;

public class Run {

	public static void executeDropTable() throws SQLException,
			URISyntaxException {
		File[] fs = SqlScriptUtils.dropFiles();
		ExecuteUtils exe = null;
		try {
			exe = ExecuteUtils.getExecuter();
			for (File f : fs) {
				SqlScriptUtils.execute(f, exe);
			}
		} catch (Exception e) {

		} finally {
			exe.release();
		}
	}

	public static void executeCreateTable() throws Exception {
		File[] fs = SqlScriptUtils.createFiles();
		ExecuteUtils exe = ExecuteUtils.getExecuter();
		try {
			for (File f : fs) {
				SqlScriptUtils.execute(f, exe);
			}
		} finally {
			exe.release();
		}
	}

	// 执行insert脚本
	public static void executeInsert() throws Exception {
		File[] fs = SqlScriptUtils.sortSqlFiles();
		ExecuteUtils executer = ExecuteUtils.getExecuter();
		try {
			for (File f : fs) {
				SqlScriptUtils.execute(f, executer);
			}
		} finally {
			executer.release();
		}
	}

	public static void executeSql(String[] filenames) throws Exception {
		File[] fs = SqlScriptUtils.sqlFiles(filenames);
		ExecuteUtils executer = ExecuteUtils.getExecuter();
		try {
			for (File f : fs) {
				SqlScriptUtils.execute(f, executer);
			}
		} finally {
			executer.release();
		}
	}

	public static void workflowDeploy() throws Exception {
		WorkflowUtils.newInstance().deploy();
	}

	public static void main(String[] args) throws Exception {
//		executeSql(new String[] { "menu.1.sql" });
//		executeSql(new String[] { "params.sql" });
//		executeSql(new String[] { "region.sql" });
		executeSql(new String[] { "init.3.sql" });
	}
}